define(["jquery", "easy-admin"], function ($, ea) {
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return decodeURI(pair[1]);  //处理中文显示异常
            }
        }
    }

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'mall.user/index',
        withdraw_url: 'mall.user/withdraw',
        reward_url: 'mall.user/reward',
        address_url: 'mall.user/address',
        promotion_url: 'mall.user/promotion',
        modify_url: 'mall.user/modify',
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh'],
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: '用户ID'},
                    {field: 'name', title: '姓名'},
                    {field: 'mobile', title: '电话号码'},
                    {field: 'avatar_url', title: '头像', templet: ea.table.image, search: false},
                    {field: 'success_reward_amount', title: '累计奖励', templet: ea.table.price, search: false},
                    {field: 'failure_reward_amount', title: '累计补贴', templet: ea.table.price, search: false},
                    {field: 'withdraw_amount', title: '累计提现金额', templet: ea.table.price, search: false},
                    {field: 'balance', title: '余额', templet: ea.table.price, search: false},
                    {field: 'consume_amount', title: '现金消费金额(不含退款)', templet: ea.table.price, search: false},
                    {field: 'promotion.name', title: '推广人'},
                    // {
                    //     field: 'status',
                    //     search: 'select',
                    //     selectList: ["禁用", "启用"],
                    //     title: '状态',
                    //     templet: ea.table.switch
                    // },
                    // {field: 'login_ip', title: '登录IP'},
                    {field: 'create_time', minWidth: 80, title: '创建时间', search: 'false'},
                    {
                        field: 'ea_mall_user-create_time_left',
                        minWidth: 80,
                        title: '创建开始时间',
                        search: 'rangeLeft',
                        hide: true,
                        searchOp: 'rangeLeft'
                    },
                    {
                        field: 'ea_mall_user-create_time_right',
                        minWidth: 80,
                        title: '创建结束时间',
                        search: 'rangeRight',
                        hide: true,
                        searchOp: 'rangeRight'
                    },
                    // {field: 'open_draw', title: '开启退款', selectList: {0: '否', 1: '是'}, templet: ea.table.switch},
                    {
                        width: 300,
                        title: '操作',
                        templet: function (row) {
                            var operat = [
                                [{
                                    text: '提现记录',
                                    url: init.withdraw_url,
                                    method: 'open',
                                    auth: 'withdraw',
                                    class: 'layui-btn layui-btn-normal layui-btn-xs',
                                }], [{
                                    text: '奖励记录',
                                    url: init.reward_url,
                                    method: 'open',
                                    auth: 'reward',
                                    class: 'layui-btn layui-btn-normal layui-btn-xs',
                                }], [{
                                    text: '地址管理',
                                    url: init.address_url,
                                    method: 'open',
                                    auth: 'address',
                                    class: 'layui-btn layui-btn-normal layui-btn-xs',
                                    extend: 'data-full="true"',
                                }],

                            ]
                            var promotion = row.promotion.name
                            if (promotion === '') {
                                operat.push(
                                    [
                                        {
                                            class: 'layui-btn layui-btn-success layui-btn-xs',
                                            method: 'open',
                                            field: 'id',
                                            icon: '',
                                            text: '设置推广人',
                                            title: '设置推广人',
                                            auth: 'send',
                                            url: init.promotion_url,
                                            extend: ''
                                        }
                                    ]
                                )
                            }

                            return ea.table.tool(row, {
                                init, // 初始化的数据变量
                                operat: operat
                            }); // 其他状态下 没有操作按钮返回空字符串即可
                        },
                    },
                ]],
            });

            ea.listen();
        },
        withdraw: function () {
            ea.table.render({
                elem: '#withdrawTable',
                url: 'withdraw',
                where: {id: getQueryVariable('id')},
                init: init,
                toolbar: [
                    'refresh'
                ],
                cols: [[
                    // {type: 'checkbox'},
                    {field: 'id', title: 'id', search: false},
                    {field: 'amount', title: '提现金额', templet: ea.table.price, search: false},
                    {
                        field: 'status',
                        width: 150,
                        title: '状态',
                        selectList: {0: '审核中', 1: '打款中', 2: '提现成功', 3: '奖励金额', 10: '提现被驳回'}
                    },
                    {field: 'balance', width: 150, title: '变动后余额', templet: ea.table.price, search: false},
                    {field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'},
                ]],
            });

            ea.listen();
        },
        reward: function () {
            ea.table.render({
                url: 'reward',
                where: {id: getQueryVariable('id')},
                init: init,
                toolbar: ['refresh'],
                cols: [[
                    // {type: 'checkbox'},
                    {field: 'mallOrder.order_no', title: '订单号', search: false},
                    {field: 'amount', title: '奖励金额', templet: ea.table.price, search: false},
                    {field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'},
                ]],
            });
            ea.listen();
        },
        address: function () {
            ea.table.render({
                url: 'address',
                where: {id: getQueryVariable('id')},
                init: init,
                toolbar: ['refresh'],
                cols: [[
                    // {type: 'checkbox'},
                    {field: 'name', title: '收货人姓名'},
                    {field: 'phone', title: '联系电话'},
                    {field: 'province', minWidth: 50, title: '省份'},
                    {field: 'city', minWidth: 50, title: '城市'},
                    {field: 'region', minWidth: 50, title: '地区'},
                    {field: 'detail', title: '详细地址'},
                    {
                        field: 'current', width: 80, title: '默认地址', selectList: {0: '否', 1: '是'},
                    },
                ]],
            });
            ea.listen();
        },
        promotion: function () {
            ea.listen();
        }
    };
    return Controller;
})
;